package com.leyou.item.web;

import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/5/5 0005
 * Time: 19:48
 */

@RestController
@RequestMapping("sku")
public class SkuController {
    @Autowired
    private SkuService skuService;

    /**
     * 根据spu_id查询sku 返回list集合
     *
     * @param id spu_id
     * @return 返回sku-list
     */
    @GetMapping("/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(skuService.querySkuBySpuId(id));
    }

    @PostMapping("goods")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO) {
        skuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @PutMapping("goods")
    public ResponseEntity<Void> editGoods(@RequestBody SpuDTO spuDTO) {
        skuService.editGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping("list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(skuService.querySkuByIds(ids));
    }

    @PutMapping("stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> cartMap) {
        skuService.minusStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
